<script setup lang="ts">
import { ref, reactive, onMounted, computed, watch } from 'vue';
import { storeToRefs } from 'pinia'
import { useRouter, useRoute } from 'vue-router'
import { getHotSongListDetailData, getHotSongListData } from "@/api/api"
import type { RankSongsDetailsType, SongListType, CollectSongListType } from '../types'
import { usePlayerStore } from '../stores/player'
import SongList from './SongList.vue'
import { Toast } from 'vant';

const songListId = ref(0)
const title = ref('')
const description = ref('')
const coverImgUrl = ref('')
const nickname = ref('')
const avatarUrl = ref('')
const songLength = ref(0)
const isShowCollect = ref(false)
const isShow = ref(false)
const pageDom = ref()
const navDom = ref()
const style = reactive({
    backgroundColor: '',
})
const route = useRoute()
const router = useRouter()
const store = usePlayerStore()
const { collectMusic } = storeToRefs(store)
const collectSongList = reactive<CollectSongListType>({
    playlist: {
        subscribed: false
    }
})
onMounted(async () => {
    let songId = route.query.id
    songListId.value = parseInt(songId as string)

    // 获取歌单详情
    if (songListId.value) {
        let songDetails = sessionStorage[songListId.value]
        if (songDetails) {
            songDetails = JSON.parse(songDetails)
            collectSongList.playlist = songDetails.collectSongList,
                coverImgUrl.value = songDetails.coverImgUrl,
                title.value = songDetails.title,
                description.value = songDetails.description,
                nickname.value = songDetails.nickname,
                avatarUrl.value = songDetails.avatarUrl,
                isShowCollect.value = songDetails.isShowCollect
        } else {
            let { data: songDetailsData } = await getHotSongListDetailData(songListId.value)

            collectSongList.playlist = songDetailsData.playlist
            coverImgUrl.value = songDetailsData.playlist.coverImgUrl
            title.value = songDetailsData.playlist.name
            description.value = songDetailsData.playlist.description
            nickname.value = songDetailsData.playlist.creator.nickname
            avatarUrl.value = songDetailsData.playlist.creator.avatarUrl
            // isShowCollect.value = songDetailsData.playlist.subscribed
            saveSongDetailsDataUseSessionStorage(songListId.value)
        }

    } else {

        getCollectMusicItemData()

    }



    // 监听页面高度
    pageDom.value.addEventListener('scroll', scrollTop, true)

})
// 定义喜欢的音乐标题详情
const getCollectMusicItemData = () => {
    title.value = "我喜欢的音乐"
    description.value = "音乐使人心情愉快~~"

}
// 保存到本地存储
const saveSongDetailsDataUseSessionStorage = (id: number) => {
    let key = id
    let value = {
        collectSongList: collectSongList.playlist,
        coverImgUrl: coverImgUrl.value,
        title: title.value,
        description: description.value,
        nickname: nickname.value,
        avatarUrl: avatarUrl.value,
        isShowCollect: isShowCollect.value,
    }
    sessionStorage[key] = JSON.stringify(value)

}
// 监听页面滚动到某个位置
const scrollTop = () => {
    let scroll = pageDom.value.scrollTop
    if (scroll > 100) {
        style.backgroundColor = '#111'
    }
    else if (scroll <= 100) {
        style.backgroundColor = 'transparent'

    }

}
// 回退
const onClickLeft = () => {
    router.go(-1)
}

// 动态显示喜欢的音乐背景图片
const compSongBackgroundImg = computed(() => {
    let songItem
    if (collectMusic.value.length) {

        songItem = collectMusic.value[0].al.picUrl
    }

    return songListId.value ? coverImgUrl.value : songItem
})

// 收藏歌单
const addCollectSongList = () => {
  
    isShow.value = !isShow.value 
    let key = songListId.value
    let value = {
        collectSongList: collectSongList.playlist,
        coverImgUrl: coverImgUrl.value,
        title: title.value,
        description: description.value,
        nickname: nickname.value,
        avatarUrl: avatarUrl.value,
        isShowCollect: isShow.value,
        id:songListId.value,
        musicLength:songLength.value
    }
    sessionStorage[key] = JSON.stringify(value)
 let songDetails = sessionStorage[songListId.value]
        songDetails = JSON.parse(songDetails)
    if (isShow.value) {
        Toast({
            message: '已收藏',
            position: 'top',
        });
       
        store.addLikeSongListActions(songDetails)

    } else {
        Toast({
            message: '已取消收藏',
            position: 'top',
        });
        store.delLikeSongListActions(songDetails)

    }
}
// 获取歌单歌曲列表长度
const getSongLength = (musicLength:number) =>{
    songLength.value = musicLength
}
watch(
    () => isShowCollect.value,
    (newVal: boolean) => {
        isShow.value = newVal

    }
)
</script>
<template >
    <div class="song-details-container" ref="pageDom">
        <!-- 导航栏 -->
        <van-nav-bar :style="style" ref="navDom" left-arrow fixed :border="false" @click-left="onClickLeft">
            <template #right>
                <van-icon name="ellipsis" size="20" />
            </template>
        </van-nav-bar>


        <div class="header">
            <van-image lazy-load :src="compSongBackgroundImg" />

            <!-- 文字描述 -->
            <div class="text">
                <p class="pub van-ellipsis">{{ title }}</p>
                <p class="desc van-ellipsis">{{ description }}</p>
            </div>
            <!-- 模糊程度区域 -->
            <div class="header-content">
                <div class="title">
                </div>
            </div>
        </div>
        <div class="author" v-if="songListId">
            <div class="author-left">
                <van-image round lazy-load :src="avatarUrl" />
                <p class="name">{{ nickname }}</p>

            </div>
            <div class="author-right">
                <van-icon name="like" size="22" @click="addCollectSongList" :class="{ active: isShow }" />
                <van-icon name="share" size="22" />

            </div>
        </div>
        <div class="song-list" v-if="songListId">
            <keep-alive>
                <SongList :title="title" :rankId="songListId" @set-song-length="getSongLength"></SongList>
            </keep-alive>
        </div>
        <div class="song-list" v-else>
            <SongList :title="title"></SongList>
        </div>

    </div>
</template>
<style lang="scss">
.song-details-container {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    padding-bottom: 50px;
    width: 100vw;
    height: 100vh;
    background-color: #111;
    z-index: 2;
    overflow-y: auto;


    .van-nav-bar {
        background: transparent;

        .van-icon {
            font-size: 20px;
            color: #ddd;
        }

        .van-icon-ellipsis {
            transform: rotate(90deg)
        }
    }

    .header {
        position: relative;
        width: 100vw;
        height: 40%;

        .van-image {
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            display: block;
            width: 100%;
            height: 100%;

        }

        .text {
            position: absolute;
            left: 0;
            bottom: 0px;
            padding: 0 16px;
            width: 100vw;
            height: 90px;
            font-size: 20px;
            letter-spacing: 1px;
            color: #ddd;
            z-index: 11;
            background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0), #111);

            .pub {
                margin-top: 20px;
            }

            .desc {
                margin-top: 15px;
                font-size: 13px;
                color: #9c9797;
            }
        }

        .header-content {
            position: absolute;
            left: 0;
            bottom: -15px;
            width: 100vw;
            height: 90px;
            background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0), #111);

            .title {
                position: absolute;
                top: 0;
                left: -5%;
                width: 105%;
                height: 90%;
                text-align: center;
                background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5), #111);
                filter: blur(5px);
                z-index: 10;

            }
        }

    }

    .author {
        position: sticky;
        display: flex;
        align-items: center;
        padding: 0 16px;
        width: 100vw;
        height: 50px;
        font-size: 14px;
        color: #fff;
        // transform: translateY(-10px);
        background-color: #111;
        z-index: 12;

        .author-left {
            flex: 1;
            display: flex;
            align-items: center;

            .van-image {
                margin-right: 10px;
                width: 30px;
                height: 30px;
                border-radius: 999px;

            }

            .name {
                color: #ccc;
            }
        }

        .author-right {
            flex: 1;
            display: flex;
            justify-content: flex-end;
            align-items: center;

            .van-icon {
                &:nth-of-type(2) {
                    margin-left: 20px;
                }

                &.active {
                    color: red;
                }
            }
        }

    }

    .song-list {
        margin: 11px 16px 0;
        color: #ccc;

    }
}
</style>